package com.kaka.bus.engine.home.info.question;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.QuestionAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.QuestionBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/11/7.
 */

public class QuestionActivity extends BaseActivity<QuestionPresenter> implements QuestionContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.question_recy)
    RecyclerView mQuestionRecy;
    @BindView(R.id.question_smart)
    SmartRefreshLayout mQuestionSmart;
    @BindView(R.id.question_muti)
    MutipleLayout mQuestionMuti;

    private QuestionAdapter mQuestionAdapter;

    private List<QuestionBean> mDatas = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("常见问题");

        mQuestionRecy.setLayoutManager(new LinearLayoutManager(this));
        mQuestionAdapter = new QuestionAdapter(R.layout.item_question, mDatas);
        mQuestionRecy.setAdapter(mQuestionAdapter);
        mQuestionAdapter.bindToRecyclerView(mQuestionRecy);
        mQuestionMuti.setEmptyText("您当前暂无常见问题!");
        mQuestionMuti.setStatus(MutipleLayout.Loading);
        mQuestionMuti.setEmptyImage(R.mipmap.empty_view);
    }

    @Override
    protected void initData() {
        getData();
    }

    public void getData() {
        mDatas.clear();
        mPresenter.initData(getUserId(), getToken());
    }

    @Override
    public void onError() {
        mQuestionSmart.finishLoadmore(0);
        mQuestionMuti.setStatus(MutipleLayout.Error);
    }

    @Override
    public void onResultStart(HttpWrapper<List<QuestionBean>> data) {
        mQuestionSmart.finishRefresh(0);
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            if (data.data != null && data.data.size() > 0) {
                mQuestionAdapter.addData(data.data);
                mQuestionMuti.setStatus(MutipleLayout.Success);
            } else {
                mQuestionMuti.setStatus(MutipleLayout.Empty);
            }
        } else if (Constant.ERROR_CODE.equals(data.getCode())) {
            showMessage(data.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    protected void initListener() {
        mQuestionSmart.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        mQuestionSmart.setOnLoadmoreListener(refreshlayout -> {
            mQuestionSmart.finishLoadmore(0);
            showMessage("暂无更多");
        });

        mQuestionAdapter.setOnItemClickListener((adapter, view, position) -> {
            QuestionBean data = (QuestionBean) adapter.<QuestionBean>getData().get(position);
            data.setOpen(!data.isOpen());
            mQuestionAdapter.notifyItemChanged(position);
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_question;
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
